/**
 * Created by shenguolin on 2023/8/22 9:26
 * lowcode-front
 * Description:
 */
import store from '@/store'

const mixin = {
  data() {
    return {
      search: {
        projectId: ''
      }
    }
  },
  computed: {
    changeHeaderProject() {
      return store.state.settings.changeHeaderProject
    }
  },
  watch: {
    changeHeaderProject: {
      immediate: false,
      handler: function(val) {
        if (!this.$headerSearch) {
          return
        }
        if (val != '') {
          //如果选择的不是 所有项目
          //默认key是projectId 清空sectionId
          let key = 'projectId'
          let key2 = 'sectionId'
          if (this.title == '任务管理') {
            key = 'taskHappenProjectId'
          }
          if (this.title == '会议管理'||this.title == '招标管理') {
            key = 'projectIds'
          }
          this.$set(this.search, key, val)
          this.$set(this.search, key2, '')
          // if()
          this.$nextTick(() => {
            if (this.$refs.treeData) {
              this.$refs.treeData.setCurrentKey(val)
            }
            if(this.defaultTreeKey){
              this.$set(this,'defaultTreeKey',[])
              if(this.title == '总体进度'||this.publicSearchType == '标段默认选中') {
                this.leftList.forEach((item,index)=>{
                  if(item.id == val&&item.hasOwnProperty('children')&&item.children.length >0){
                    this.defaultTreeKey.push(item.id)
                    this.$refs['treeData'].setCurrentKey(item.children[0].id);
                    this.handleNodeClick(item.children[0])
                  }
                })
              }else {
                this.defaultTreeKey.push(val)
              }
            }
            if(this.title == '总体进度'){
              this.changeDate()
            } else if(this.title == '文件管理'){
              this.getTree()
            }else if(this.title != '质检资料') {
              this.getList(true)
            }
          })

        } else {
          //不限项目
          let key = 'projectId'
          let key2 = 'sectionId'
          if (this.title == '任务管理') {
            key = 'taskHappenProjectId'
          }
          if (this.title == '会议管理'||this.title == '招标管理') {
            key = 'projectIds'
          }
          if (this.leftList && this.leftList.length > 0) {
            if(this.title == '总体进度'||this.publicSearchType == '标段默认选中') {
              this.$set(this.search, key, this.leftList[0].id)
              this.$set(this.search, key2, '')
              this.$nextTick(() => {
                if (this.$refs.treeData) {
                  this.leftList.forEach((item,index)=>{
                    if(item.id == this.leftList[0].id&&item.hasOwnProperty('children')&&item.children.length >0){
                      this.defaultTreeKey.push(item.id)
                      this.$refs['treeData'].setCurrentKey(item.children[0].id);
                      this.handleNodeClick(item.children[0])
                    }
                  })
                }
                if(this.title == '总体进度') {
                  this.changeDate()
                }
              })
            }else {
              this.$set(this.search, key, this.leftList[0].id)
              this.$set(this.search, key2, '')
              this.$nextTick(() => {
                if (this.$refs.treeData) {
                  this.$refs.treeData.setCurrentKey(this.leftList[0].id)
                  if(this.defaultTreeKey){
                    this.$set(this,'defaultTreeKey',[])
                    this.defaultTreeKey.push(this.leftList[0].id)
                  }
                }
                this.getList(true)
              })
            }

          } else {
            //如果没有leftList
            if (this.title == '文件管理') {
              this.$set(this.search, key, this.projectTree[0].id)
              this.$set(this.search, key2, '')
              this.$nextTick(() => {
                this.getTree()
              })
            } else {
              this.$set(this.search, key, val)
              this.$set(this.search, key2, '')
              this.$nextTick(() => {
                this.getList(true)
              })
            }
          }
        }

      }
    }
  },
  methods: {}
}
export default mixin
